package com.conv.shop.business.service;


import com.conv.shop.business.domain.*;
import com.conv.shop.business.mapper.*;
import com.conv.shop.business.resp.OrderResp;
import com.conv.shop.business.resp.StoreGetOrderDetailResp;
import com.conv.shop.business.util.Random;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {
    @Resource
    ConsumerMapper consumerMapper;

    @Resource
    OrderMapper orderMapper;

    @Resource
    OrderDetailMapper orderDetailMapper;

    @Resource
    ShoppingCartMapper shoppingCartMapper;

    @Resource
    ProductMapper productMapper;

    @Resource
    StoreUserMapper storeUserMapper;

    /**
     * 生成订单
     * @param consumerId
     * @return Order
     */
    public Order OrderMaker(Integer consumerId){
        ShoppingCartExample example = new ShoppingCartExample();
        double whole = 0.0;
        example.createCriteria().andConsumerIdEqualTo(consumerId).andSelectedEqualTo(1);
        List<ShoppingCart> carts = shoppingCartMapper.selectByExample(example);
        if(carts.isEmpty())
            return null;
        for(ShoppingCart cart : carts){
            Product product = productMapper.selectByPrimaryKey(cart.getProductId());
            whole += cart.getNumber() * product.getPrice();
        }
        Order order = new Order();
        order.setConsumerId(consumerId);
        order.setOrderNumber(Random.getRandomString(12));
        order.setTotalAmount(whole);
        order.setOrderStatus(0);
        order.setOrderPlacedTime(new Date(System.currentTimeMillis()));
        orderMapper.insertSelective(order);
        for(ShoppingCart cart : carts){
            Product product = productMapper.selectByPrimaryKey(cart.getProductId());
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderNumber(order.getOrderNumber());
            orderDetail.setProductId(cart.getProductId());
            orderDetail.setPurchaseQuantity(cart.getNumber());
            orderDetail.setUnitPrice(product.getPrice());
            orderDetailMapper.insert(orderDetail);
            shoppingCartMapper.deleteByPrimaryKey(cart.getShoppingCartId());
        }
        return order;
    }

    /**
     * 获取订单列表
     * @param consumerId
     * @return List<OrderResp>
     */
    public List<OrderResp> getOrderList(Integer consumerId){
        List<OrderResp> orders = new ArrayList<>();
        OrderExample example = new OrderExample();
        example.createCriteria().andConsumerIdEqualTo(consumerId).andOrderStatusEqualTo(0);
        for(Order order : orderMapper.selectByExample(example)){
            OrderResp orderResp = new OrderResp();
            orderResp.setOrderId(order.getOrderId());
            orderResp.setOrderNumber(order.getOrderNumber());
            orderResp.setOrderStatus(order.getOrderStatus());
            orderResp.setTotalAmount(order.getTotalAmount());
            orderResp.setPaymentMethod(order.getPaymentMethod());
            orderResp.setPaymentCompletedTime(order.getPaymentCompletedTime());
            orders.add(orderResp);
        }
        return orders;
    }

    /**
     * 更新订单编号查询商品id
     * @param orderNumber
     * @return int
     */
    public int getProductId (String orderNumber){
        OrderDetailExample orderDetailExample = new OrderDetailExample();
        orderDetailExample.createCriteria().andOrderNumberEqualTo(orderNumber);
         return orderDetailMapper.selectByExample(orderDetailExample).get(0).getProductId();
    }

    /**
     * 根据订单编号查询订单
     * @param orderNumber
     * @return Order
     */

    public Order getOrderByOrderNumber(String orderNumber){
        OrderExample example = new OrderExample();
        example.createCriteria().andOrderNumberEqualTo(orderNumber);
        return orderMapper.selectByExample(example).get(0);
    }

    /**
     *  店家端获取订单详情
     * @param customerId
     * @return List<StoreGetOrderDetailResp>
     */
    public List<StoreGetOrderDetailResp> getOrderDetailStore(int customerId){
        List<StoreGetOrderDetailResp> orderDetailResps = new ArrayList<>();
        StoreUserExample storeUserExample = new StoreUserExample();
        storeUserExample.createCriteria().andConsumerIdEqualTo(customerId);
        List<StoreUser> storeUsers= storeUserMapper.selectByExample(storeUserExample);
        int storeId = storeUsers.get(0).getStoreId();
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andStoreIdEqualTo(storeId);
        List<Product> products = productMapper.selectByExample(productExample);
        for (Product product : products){
            int productId = product.getProductId();
            OrderDetailExample example = new OrderDetailExample();
            example.createCriteria().andProductIdEqualTo(productId);
            List<OrderDetail> orderDetails = orderDetailMapper.selectByExample(example);
            for (OrderDetail orderDetail : orderDetails){
                StoreGetOrderDetailResp storeGetOrderDetailResp = new StoreGetOrderDetailResp();
                storeGetOrderDetailResp.setDetailId(orderDetail.getDetailId());
                OrderExample orderExample = new OrderExample();
                orderExample.createCriteria().andOrderNumberEqualTo(orderDetail.getOrderNumber());
                Order order = orderMapper.selectByExample(orderExample).get(0);
                storeGetOrderDetailResp.setOrder(order);
                storeGetOrderDetailResp.setPhone(consumerMapper.selectByPrimaryKey(order.getConsumerId()).getPhone());
                storeGetOrderDetailResp.setPurchaseQuantity(orderDetail.getPurchaseQuantity());
                storeGetOrderDetailResp.setUnitPrice(orderDetail.getUnitPrice());
                storeGetOrderDetailResp.setProduct(productMapper.selectByPrimaryKey(productId));
                orderDetailResps.add(storeGetOrderDetailResp);
            }
        }

        return orderDetailResps;
    }
}
